package com.fenxiangle.yueding.feature.identification.model;

import android.text.TextUtils;

import com.fenxiangle.yueding.common.httprequestbody.HttpRequestBody;
import com.fenxiangle.yueding.entity.bo.IDCardBo;
import com.fenxiangle.yueding.entity.bo.IdentificationBo;
import com.fenxiangle.yueding.feature.identification.contract.IdentificationContract;
import com.fenxiangle.yueding.feature.identification.login.DaggerIdentificationModelComponent;
import com.fenxiangle.yueding.framework.api.ApiLib;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.entity.bo.Result;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Home on 2018/9/9.
 */

public class IdentificationModel implements IdentificationContract.Model {
    @Inject
    UserApi api;


    @Inject
    public IdentificationModel() {
        DaggerIdentificationModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<String> add_basic_authentication(List<File> files, Map<String, String> string) {
        Map<String, RequestBody> params = new HashMap<>();
        for (int i = 0; i < files.size(); i++) {
            // create RequestBody instance from file
//            RequestBody  requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            params.put("userHead" + "\"; filename=\"" + files.get(i).getName(), RequestBody.create(MediaType.parse("multipart/form-data"), files.get(i)));
            //一定要加("AttachmentKey\"; filename=\"" +，不然失败
//            params.put(imgStr.getKey(),requestFile);
        }

        for (Map.Entry<String, String> imgStr : string.entrySet()) {
            // create RequestBody instance from file
            RequestBody requestFile = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), imgStr.getValue());
            //一定要加("AttachmentKey\"; filename=\"" +，不然失败
            params.put(imgStr.getKey(), requestFile);
        }
        return api.add_basic_authentication(params).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<IdentificationBo> select_authentication_data(String flag) {
        return api.select_authentication_data(flag).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> add_boss_authentication(List<File> files, Map<String, String> string) {
        Map<String, RequestBody> params = new HashMap<>();
        for (int i = 0; i < files.size(); i++) {
            // create RequestBody instance from file
//            RequestBody  requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            params.put("userHead" + "\"; filename=\"" + files.get(i).getName(), RequestBody.create(MediaType.parse("multipart/form-data"), files.get(i)));
            //一定要加("AttachmentKey\"; filename=\"" +，不然失败
//            params.put(imgStr.getKey(),requestFile);
        }

        for (Map.Entry<String, String> imgStr : string.entrySet()) {
            // create RequestBody instance from file
            RequestBody requestFile = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), imgStr.getValue());
            //一定要加("AttachmentKey\"; filename=\"" +，不然失败
            params.put(imgStr.getKey(), requestFile);
        }
        return api.add_boss_authentication(params).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<IDCardBo> add_uploadIDCard_authentication(String cardNegative, String cardPositive) {
        MultipartBody.Part cardNegativePart = null;
        MultipartBody.Part cardPositivePart = null;

        if (!TextUtils.isEmpty(cardNegative)){
            File headFile = new File(cardNegative);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            cardNegativePart = MultipartBody.Part.createFormData("cardPositive", headFile.getName(), requestFile);
        }
        if (!TextUtils.isEmpty(cardPositive)){
            File headFile = new File(cardNegative);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            cardPositivePart = MultipartBody.Part.createFormData("cardNegative", headFile.getName(), requestFile);
        }
        return api.add_uploadIDCard_authentication(cardNegativePart,cardPositivePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<IDCardBo> add_uploadBusinessLicense_authentication(String businessLicense) {
        MultipartBody.Part businessLicensePart = null;
        if (!TextUtils.isEmpty(businessLicense)){
            File headFile = new File(businessLicense);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            businessLicensePart = MultipartBody.Part.createFormData("businessLicense", headFile.getName(), requestFile);
        }
        return api.add_uploadBusinessLicense_authentication(businessLicensePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}
